package com.example.select_lesson;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.balysv.materialmenu.MaterialMenuDrawable;


public class StudentInformationActivity extends ActionBarActivity {

    public static void actionStart(Context context, String studentId) {
        Intent intent = new Intent(context, StudentInformationActivity.class);
        intent.putExtra("student_id", studentId);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        context.startActivity(intent);
    }

    private MaterialMenuDrawable materialMenu;
    private TextView name;
    private TextView id;
    private TextView sex;
    private TextView classNumber;
    private TextView choosedClass;
    private String studentId;

    private String studentName;
    private String studentSex;
    private String studentClass;
    private SQLiteDatabase db;
    private MyDataBaseHelper dbHelper;

    private int choosedClassNumber = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_student_information);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(getResources().getColor(R.color.my_dark_blue));
        }
        studentId = getIntent().getStringExtra("student_id");
        // 数据库
        dbHelper = new MyDataBaseHelper(this, "StudentManager.db", null, 1);
        dbHelper.getWritableDatabase();
        db = dbHelper.getWritableDatabase();
        Cursor cursor = db.rawQuery("select * from Student where studentId=?",
                new String[] {studentId});
        if (cursor.moveToFirst()) {
            do {
                studentName = cursor.getString(cursor.getColumnIndex("name"));
                studentSex = cursor.getString(cursor.getColumnIndex("sex"));
                studentClass = cursor.getString(cursor.getColumnIndex("class"));
            } while (cursor.moveToNext());
        }
        cursor.close();

        cursor = db.rawQuery("select * from SELECT_LESSON where studentId=?",
                new String[] {studentId});
        if (cursor.moveToFirst()) {
            do {
                choosedClassNumber++;
            } while (cursor.moveToNext());
        }
        cursor.close();

        init();

        setToolbar();

    }

    private void setToolbar() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("  我的信息");
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                switch (materialMenu.getIconState()) {
                    case BURGER:
                        break;
                    case ARROW:
                        materialMenu.animateIconState(MaterialMenuDrawable.IconState.ARROW, false);
                        finish();
                        break;
                    case X:
                        break;
                    case CHECK:
                        break;
                }

            }
        });
        materialMenu = new MaterialMenuDrawable(this, Color.WHITE, MaterialMenuDrawable.Stroke.THIN);
        materialMenu.animateIconState(MaterialMenuDrawable.IconState.ARROW, false);
        toolbar.setNavigationIcon(materialMenu);
        materialMenu.setNeverDrawTouch(true);
    }
    private void init() {
        name = (TextView) findViewById(R.id.tv_name);
        id = (TextView) findViewById(R.id.tv_id);
        classNumber = (TextView) findViewById(R.id.tv_class);
        sex = (TextView) findViewById(R.id.tv_sex);
        choosedClass = (TextView) findViewById(R.id.tv_choosed_class);

        name.setText("姓名：" + studentName);
        id.setText("学号：" + studentId);
        classNumber.setText("班级：" + studentClass);
        sex.setText("性别：" + studentSex);
        choosedClass.setText("已选课程：" + choosedClassNumber + " 节");
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_student_information, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement

        return super.onOptionsItemSelected(item);
    }
}
